﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using HGZP.BLL;
using HGZP.DAL;
using HGZP.Model;
using HGZP.PLL;

namespace SZCB.admin.EnterpriseManager
{
    public partial class AddEnterprise : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                BindFoodClass();
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CEnterpriseBLL myCEnterpriseBLL = new CEnterpriseBLL();
            DataTable dt = myCEnterpriseBLL.F02_SelectHaveWhere("sName = '" + txtName.Text + "'");
            if (dt.Rows.Count > 0)
            {
                string strErrmessage = "该企业已添加";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }
            if (ddlContentClass.SelectedItem.Text == "----请选择分类----")
            {
                string strErrmessage = "请选择食品大类";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;

            }


            CEnterprise myCEnterprise = new CEnterprise();
            myCEnterprise.sGuid = MyFunction.GetNewGuid();
            myCEnterprise.sLargeClass = ddlContentClass.SelectedItem.Text;
            myCEnterprise.sLargeClassGuid = ddlContentClass.SelectedValue;
            myCEnterprise.sAddress = txtAddress.Text;
            myCEnterprise.sName = txtName.Text;
            myCEnterprise.sPhone = txtPhone.Text;
            myCEnterprise.dCreateDate = DateTime.Now;
            myCEnterprise.sCountryGuid = DropDownListContry.SelectedValue;
            myCEnterprise.sCountryName = DropDownListContry.SelectedItem.Text;
            myCEnterprise.sProvinceGuid = DropDownListPrivnce.SelectedValue;
            myCEnterprise.sProvinceName = DropDownListPrivnce.SelectedItem.Text;
            myCEnterprise.sCityGuid = DropDownListCity.SelectedValue;
            myCEnterprise.sCityName = DropDownListCity.SelectedItem.Text;

            if (!myCEnterpriseBLL.F_Insert(myCEnterprise))
            {
                string strErrmessage = "添加企业失败";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }


        }

        private void BindFoodClass()
        {
            CProductClassBLL myCProductClassBLL = new CProductClassBLL();
            ddlContentClass.DataSource = myCProductClassBLL.F02_SelectHaveWhere("");
            ddlContentClass.DataTextField = myCProductClassBLL.sName;
            ddlContentClass.DataValueField = myCProductClassBLL.sGuid;
            ddlContentClass.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择分类----";
            listItem.Value = "0";
            ddlContentClass.Items.Add(listItem);
            ddlContentClass.SelectedValue = "0";

            CCountryBLL myCCountryBLL = new CCountryBLL();
            DropDownListContry.DataSource = myCCountryBLL.F02_SelectHaveWhere("");
            DropDownListContry.DataTextField = myCCountryBLL.sName;
            DropDownListContry.DataValueField = myCCountryBLL.sGuid;
            DropDownListContry.DataBind();
            listItem = new ListItem();
            listItem.Text = "----请选择分类----";
            listItem.Value = "0";
            DropDownListContry.Items.Add(listItem);
            DropDownListContry.SelectedValue = "0";


            CProvinceBLL myCProvinceBLL = new CProvinceBLL();
            DropDownListPrivnce.DataSource = myCProvinceBLL.F02_SelectHaveWhere("");
            DropDownListPrivnce.DataTextField = myCProvinceBLL.sName;
            DropDownListPrivnce.DataValueField = myCProvinceBLL.sGuid;
            DropDownListPrivnce.DataBind();
            listItem = new ListItem();
            listItem.Text = "----请选择分类----";
            listItem.Value = "0";
            DropDownListPrivnce.Items.Add(listItem);
            DropDownListPrivnce.SelectedValue = "0";


            CCityBLL myCCityBLL = new CCityBLL();
            DropDownListCity.DataSource = myCCityBLL.F02_SelectHaveWhere("");
            DropDownListCity.DataTextField = myCCityBLL.sName;
            DropDownListCity.DataValueField = myCCityBLL.sGuid;
            DropDownListCity.DataBind();
            listItem = new ListItem();
            listItem.Text = "----请选择分类----";
            listItem.Value = "0";
            DropDownListCity.Items.Add(listItem);
            DropDownListCity.SelectedValue = "0";


        }


    }
}